using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;

namespace Company.Interface.GC
{
    public partial class SanPhamRegistedGCForm : BaseForm
    {
        public SanPham SanPhamSelected;
        private SanPhamCollection spCollection;
       

        public SanPhamRegistedGCForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            spCollection=SanPhamSelected.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
            dgList.DataSource = spCollection;
        }

        //-----------------------------------------------------------------------------------------
        
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();          
            lblHint.Visible = this.CalledForm != string.Empty;
        }

        //-----------------------------------------------------------------------------------------
        
        private void SanPhamRegistedForm_Load(object sender, EventArgs e)
        {           
            // Sản phẩm đã đăng ký.
            this.BindData();
            // Doanh nghiệp / Đại lý TTHQ.            
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                e.Row.Cells["TenDVT"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
                e.Row.Cells["TenNhom"].Text = this.NhomSanPham_GetName(e.Row.Cells["NhomSanPham"].Value.ToString());
            }

        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    this.Cursor = Cursors.WaitCursor;
            //    btnGetListFromHQ.Enabled = false;
            //    this.dsRegistedList = new SanPham().WS_GetDanhSachDaDangKy(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
            //    dgList.DataSource = this.dsRegistedList.Tables[0];
            //    // Cập nhật vào CSDL.
            //   // bool ret = new SanPham().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, this.dsRegistedList);
            //    if (ret)
            //    {
            //        this.ShowMessage("Cập nhật thành công", false);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    this.ShowMessage("Có lỗi: " + ex.Message, false);
            //}
            //finally
            //{
            //    btnGetListFromHQ.Enabled = true;
            //    this.Cursor = Cursors.Default;
            //}
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (lblHint.Visible)
            {
                if (e.Row.RowType == RowType.Record)
                {
                    string maSP = e.Row.Cells["Ma"].Text;
                    this.SanPhamSelected.Ma = maSP;
                    this.SanPhamSelected.Ten = e.Row.Cells["Ten"].Text;
                    this.SanPhamSelected.MaHS = e.Row.Cells["MaHS"].Text;
                    this.SanPhamSelected.DVT_ID = e.Row.Cells["DVT_ID"].Text;
                    this.SanPhamSelected.NhomSanPham = e.Row.Cells["NhomSanPham"].Text;  
                    this.SanPhamSelected.SoLuongDangKy=Convert.ToDecimal(e.Row.Cells["SoLuongDangKy"].Text);  
                    this.Close();
                }
            }
            else 
            {

            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //-----------------------------------------------------------------------------------------
    }
}